package com.zeroinverse.fortunecookie;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.SimpleCursorAdapter;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class FortuneCookieHistoryListActivity extends ListActivity {

	private FortuneMessageDatabaseAdapter dbHelper = null;
	private SimpleCursorAdapter history;
	private Cursor cursor;

	private static final int MENU_ID_SHARE = Menu.FIRST;
	private static final int MENU_ID_DELETE = Menu.FIRST + 1;

	public FortuneCookieHistoryListActivity() {
		// TODO Auto-generated constructor stub
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history);
		
		/*
		AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);
		//AdWhirlTargeting.setAge(20);
		AdWhirlTargeting.setTestMode(false);

		AdWhirlLayout adWhirlLayout = (AdWhirlLayout)findViewById(R.id.ad2);
		adWhirlLayout.setAdWhirlInterface(this);
		*/
		AdView adView = (AdView)this.findViewById(R.id.ad2);
	    adView.loadAd(new AdRequest());
	    

		registerForContextMenu(getListView());

		if (dbHelper == null) {
			dbHelper = new FortuneMessageDatabaseAdapter(this);
			if (dbHelper == null) {
				return;
			}
		}
		dbHelper.open();
		fillData();
	}
	
	/*
	public void onStop() {
		super.onStop();
		if (dbHelper != null) {
			dbHelper.close();
			dbHelper = null;
		}
	}
	*/

	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		menu.add(Menu.NONE, MENU_ID_SHARE, Menu.NONE, getResources().getString(R.string.send));
		menu.add(Menu.NONE, MENU_ID_DELETE, Menu.NONE, getResources().getString(R.string.delete));
	}

	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		int prevPosition = cursor.getPosition();
		cursor.moveToPosition(info.position);
		
		String selectedMessage = cursor.getString(
				cursor.getColumnIndex(dbHelper.getMessageTableKeyContent()));
		
		int messageId = cursor.getInt(
				cursor.getColumnIndex(dbHelper.getMessageTableKeyId()));
		
		cursor.moveToPosition(prevPosition);
		
		if (item.getItemId() == MENU_ID_SHARE) {
			Intent it = new Intent(android.content.Intent.ACTION_SEND);
			it.setType("text/plain");
			it.putExtra(Intent.EXTRA_SUBJECT, "Fortune Cookie");
			it.putExtra(Intent.EXTRA_TEXT, "Message of Fortune Cookie: " + selectedMessage);
			startActivity(Intent.createChooser(it, getResources().getString(R.string.send)));

		} else if (item.getItemId() == MENU_ID_DELETE) {
			dbHelper.updateMessage(messageId, false, (long)0);
			fillData();
		}

		return true;
	}


	private void fillData() {
		cursor = dbHelper.fetchMessage(true);
		//Cursor cursor = dbHelper.fetchAllMessages();
		startManagingCursor(cursor);

		String[] from = new String[] {
				dbHelper.getMessageTableKeyContent()};

		int[] to = new int[] {
				R.id.content_textview};

		history = new SimpleCursorAdapter(
				this,
				R.layout.history_row,
				cursor,
				from,
				to);

		setListAdapter(history);
	}

}
